#region using

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace RealEstate.Web
{
	class Dispatcher
	{

		#region Static Members

		private static string s_strCreateLock = "{7B673098-E9EC-4f60-97BA-40095FC1F211}";

		private static Dispatcher s_objDispatcher = null;

		#endregion


		#region Constructors

		private Dispatcher() { }

		#endregion

		#region Static Methods

		public static Dispatcher Instance
		{
			get
			{
				if (s_objDispatcher != null)
					return s_objDispatcher;

				lock (s_strCreateLock)
				{
					if (s_objDispatcher != null)
						return s_objDispatcher;

					s_objDispatcher = new Dispatcher();
				}

				return s_objDispatcher;
			}
		}

		#endregion

		#region

		public void Do(Command cmd)
		{
			try
			{
				//while (true)
				//{ 

				IController controller = GetController(cmd);

				if (controller == null) throw new NullReferenceException("Returned null controller.");

				// this should pass some context
				controller.Go(new object());
			}
			catch (System.Threading.ThreadAbortException)
			{
				// do nothing
			}
			catch (Exception exp)
			{ 
				// need to do some logging
				throw exp;
			}


			//}
		}

		#endregion

		#region Private Methods

		private IController GetController(Command objCommand)
		{
			// need to implement ServiceRegistrar
			/*
			WebContext objContext = WebContext.Current;
			ControllerRegistry objReg = ControllerRegistry.GetControllerRegistry(objContext);

			return objReg.GetController(objContext, objCommand.Path, objCommand.CommandName);
			*/

			return new BaseController();
		}

		#endregion
	}
}
